package com.example.baseweb.controller;

import com.example.baseweb.common.ApiResponse;
import com.example.baseweb.common.PageResponse;
import com.example.baseweb.dto.DishDTO;
import com.example.baseweb.service.DishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/dish")
public class DishController {
    
    @Autowired
    private DishService dishService;
    
    // 获取所有菜品（如无分页需求可直接去掉）
    // @PostMapping("/list")
    // public ApiResponse<List<DishDTO>> getAllDishes(@RequestBody(required = false) Map<String, Object> params) {
    //     return ApiResponse.success(dishService.getAllDishes());
    // }
    
    // 根据ID获取菜品
    @PostMapping("/detail")
    public ApiResponse<DishDTO> getDishById(@RequestBody Map<String, Object> params) {
        Long id = Long.valueOf(params.get("id").toString());
        return ApiResponse.success(dishService.getDishById(id));
    }
    
    // 保存菜品
    @PostMapping("/save")
    public ApiResponse<DishDTO> saveDish(@RequestBody DishDTO dish) {
        return ApiResponse.success(dishService.saveDish(dish));
    }
    
    // 更新菜品
    @PostMapping("/update")
    public ApiResponse<DishDTO> updateDish(@RequestBody DishDTO dish) {
        return ApiResponse.success(dishService.updateDish(dish));
    }
    
    // 删除菜品
    @PostMapping("/delete")
    public ApiResponse<Void> deleteDish(@RequestBody Map<String, Object> params) {
        Long id = Long.valueOf(params.get("id").toString());
        dishService.deleteDish(id);
        return ApiResponse.success();
    }
    
    // 更新菜品状态（上下架）
    @PostMapping("/updateStatus")
    public ApiResponse<DishDTO> updateDishStatus(@RequestBody Map<String, Object> params) {
        Long id = Long.valueOf(params.get("id").toString());
        Integer status = Integer.valueOf(params.get("status").toString());
        return ApiResponse.success(dishService.updateDishStatus(id, status));
    }
    
    // 根据分类获取菜品（如无分页需求可直接去掉）
    // @PostMapping("/listByCategory")
    // public ApiResponse<List<DishDTO>> getDishesByCategory(@RequestBody Map<String, Object> params) {
    //     Long categoryId = Long.valueOf(params.get("categoryId").toString());
    //     return ApiResponse.success(dishService.getDishesByCategory(categoryId));
    // }
    
    // 根据状态查询菜品（如无分页需求可直接去掉）
    // @PostMapping("/listByStatus")
    // public ApiResponse<List<DishDTO>> getDishesByStatus(@RequestBody Map<String, Object> params) {
    //     Integer status = Integer.valueOf(params.get("status").toString());
    //     return ApiResponse.success(dishService.getDishesByStatus(status));
    // }
    
    // 根据分类分页查询菜品
    @PostMapping("/page")
    public ApiResponse<PageResponse<DishDTO>> getDishPage(@RequestBody Map<String, Object> params) {
        int page = params.get("page") != null ? Integer.parseInt(params.get("page").toString()) : 1;
        int size = params.get("size") != null ? Integer.parseInt(params.get("size").toString()) : 10;
        Long categoryId = params.get("category_id") != null ? Long.valueOf(params.get("category_id").toString()) : null;
        PageResponse<DishDTO> result = dishService.findDishPage(categoryId, page, size);
        return ApiResponse.success(result);
    }
} 